package com.example.myproject5

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity

const val SCORE = "SCORE"
const val REQUEST_CODE_CHANGE = 1
const val REQUEST_CODE_GAME = 2

class MainActivity : AppCompatActivity() {

    // 声明TextView变量
    private lateinit var textName: TextView
    private lateinit var textGender: TextView
    private lateinit var textNumber: TextView
    private lateinit var textPhone: TextView
    private lateinit var textAddress: TextView
    private lateinit var textHobby: TextView
    private lateinit var textScore: TextView
    private lateinit var imageViewPhoto: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        // 初始化TextView
        initViews()

        val first = findViewById<Button>(R.id.change_button)
        first.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_CHANGE)
        }

        val second = findViewById<Button>(R.id.game_button)
        second.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_GAME)
        }
    }

    private fun initViews() {
        // 找到对应的TextView
        textName = findViewById(R.id.editTextText)
        textGender = findViewById(R.id.editTextText3)
        textNumber = findViewById(R.id.editTextText7)
        textPhone = findViewById(R.id.editTextText8)
        textAddress = findViewById(R.id.editTextText4)
        textHobby = findViewById(R.id.editTextText6)
        textScore = findViewById(R.id.editTextText13)
        imageViewPhoto = findViewById(R.id.imageView)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        when (requestCode) {
            REQUEST_CODE_CHANGE -> {
                if (resultCode == Activity.RESULT_OK) {
                    // 处理从 SecondActivity 返回的数据
                    data?.let { intent ->
                        val name = intent.getStringExtra("name") ?: ""
                        val number = intent.getStringExtra("number") ?: ""
                        val phone = intent.getStringExtra("phone") ?: ""
                        val address = intent.getStringExtra("address") ?: ""
                        val gender = intent.getStringExtra("sex") ?: ""
                        val hobbies = intent.getStringArrayListExtra("hobby") ?: arrayListOf()
                        val photoData = intent.getByteArrayExtra("photo_data")
                        // 更新UI显示数据
                        updateDisplay(name, gender, number, phone, address, hobbies,photoData)
                    }
                }
            }
            REQUEST_CODE_GAME -> {
                if (resultCode == Activity.RESULT_OK) {
                    // 处理从 ThirdActivity 返回的数据
                    data?.let { intent ->
                        val score = intent.getIntExtra(SCORE, 0)
                        // 更新分数显示
                        textScore.text = score.toString()
                    }
                }
            }
        }
    }

    private fun updateDisplay(
        name: String,
        gender: String,
        number: String,
        phone: String,
        address: String,
        hobbies: List<String>,
        photoData: ByteArray?
    ) {
        textName.text = name
        textGender.text = gender
        textNumber.text = number
        textPhone.text = phone
        textAddress.text = address
        textHobby.text = hobbies.joinToString(", ")

        photoData?.let { data ->
            val imageBitmap = BitmapFactory.decodeByteArray(data, 0, data.size)
            imageBitmap?.let {
                imageViewPhoto.setImageBitmap(it)
                // 移除默认背景
                imageViewPhoto.background = null
            }
        }
    }
}